﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.EHR.ResumeParseLib;
using System.Configuration;
using DevExpress.Web.ASPxUploadControl;
using System.IO;
using DevExpress.Web.ASPxClasses.Internal;
using System.Web.Caching;
using ZX.EHR.Library;
using System.Data;
using ZX.DAL.DBExtend;

namespace ZX.WebApp.EHR
{
    public partial class hrUpLoadBasicSubsidy : BasePage
    {
        static string resFileName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                UploadControl.ShowProgressPanel = true;
            }
            if (resFileName != "") this.SaveBasicSubsidyData();
        }

        protected void UploadControl_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            try
            {
                e.CallbackData = SavePostedFiles(e.UploadedFile);
            }
            catch (Exception ex)
            {
                e.IsValid = false;
                e.ErrorText = ex.Message;
            }
        }

        string SavePostedFiles(UploadedFile uploadedFile)
        {
            try
            {
                if (!uploadedFile.IsValid)
                    return string.Empty;

                FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
                string path = Request.PhysicalApplicationPath + "upload/EHR/";
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                resFileName = path + fileInfo.Name;
                uploadedFile.SaveAs(resFileName);
                RemoveFileWithDelay(uploadedFile.FileName, resFileName, 5);
                string fileLabel = fileInfo.Name;
                string fileLength = uploadedFile.ContentLength / 1024 + "K";
                return string.Format("{0} ({1})|{2}", fileLabel, fileLength, fileInfo.Name);
            }
            catch (Exception ex)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    ZX.BLL.Extend.BaseBussnies errBll = new ZX.BLL.Extend.BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = this.GetType().Module.Name;
                    salaryErrLog.className = this.GetType().Name;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = ex.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
                throw ex;
            }
        }



        static void RemoveFileWithDelay(string key, string fullPath, int delay)
        {
            if (HttpUtils.GetCache()[key] == null)
            {
                DateTime absoluteExpiration = DateTime.Now.Add(new TimeSpan(0, delay, 0));
                HttpUtils.GetCache().Insert(key, fullPath, null, absoluteExpiration,
                    Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(RemovedCallback));
            }
        }
        static void RemovedCallback(string key, object value, CacheItemRemovedReason reason)
        {
            if (File.Exists(value.ToString()))
                File.Delete(value.ToString());
        }

        /// <summary>
        /// 保存薪资数据
        /// </summary>
        protected void SaveBasicSubsidyData()
        {
            int rs = 1;
            try
            {
                rs = EncryptData.EncryptSalary.SaveUpLoadBasicSubsidyData(resFileName, this.User.UserID);
                if (System.IO.File.Exists(resFileName))
                {
                    System.IO.File.Delete(resFileName);
                }
                resFileName = "";
            }
            catch (Exception ex)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    ZX.BLL.Extend.BaseBussnies errBll = new ZX.BLL.Extend.BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = this.GetType().Module.Name;
                    salaryErrLog.className = this.GetType().Name;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = ex.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
                resFileName = "";
            }
            HttpContext.Current.Response.Write(rs.ToString());
        }
    }
}